#include <stdio.h>
#include <stdlib.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <string.h>
#include <sys/stat.h>
#include <errno.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <pthread.h>
#include <sys/select.h>
#include "common.h"
#include "server.h"

void *thread_fun(void *arg)
{
	PK_COMMON    pack;
	PK_REQUEST   rrq;
	PK_DATA      data;
	PK_ACK       ack;
	PK_ERROR     err;
	PK_IFLIVE    live;
	LOG_LIST     *loglist;
	MSG_ERROR    ero;
	int rdret=-1;
	int wrret=-1;
	int ret=-1;
	int maxfd;
	int recvpack=-1;

	maxfd=*((int *)arg);
	do{
		rdret=read(maxfd,&pack,sizeof(struct pk_common));
		if(rdret<0)
		{
			perror("error read - ");
			return;
		}
		//printf("%d\n",pack.opt_code);
		//printf("%s\n",pack.datas);
		switch(pack.opt_code)
		{
			case 1:
				do_request(pack,maxfd,&ero,&loglist);
				break;
			case 2:
				do_data(&pack,NULL);
				break;
			case 3:
				do_ack(&pack);
				break;
			case 4:
				do_error(&pack,&ero,&loglist);
				break;
			case 5:
				do_iflive(&pack);
				break;
		}
	}while(rdret>0);
	close(*(int*)arg);
}

int do_request(PK_COMMON pack,int sock,MSG_ERROR *error,LOG_LIST **linklist)
{	PK_REQUEST  rq;
	PK_ACK     ack;
	int        count=0;
	int         i=0;
	int         datalen;
	memset(&ack,0,sizeof(struct pk_ack));
	memset(&rq,0,sizeof(struct pk_request));
	int ret=-1;
	int usrtmp;
	int psdtmp;

	//printf("sss\n");

	rq.opt_code = pack.opt_code;
	datalen=strlen(pack.datas);

	//printf("%d\n",datalen);

	char       p[datalen];
	strcpy(p,pack.datas);
	//printf("%s\n",pack.datas);
	//printf("xxx\n");
	while(p[i]!=',')
	{
		strncat(rq.username,&p[i],1);
		i++;
	}
	i++;
	while(p[i]!='\0')
	{
		strncat(rq.passwd,&p[i],1);
		i++;
	}
	
	ack.opt_code=3;
	//printf("%s\n",rq.username);
	//printf("%s\n",rq.passwd);
	usrtmp=strcmp(rq.username,"admin");
	psdtmp=strcmp(rq.passwd,"123456");
	if(usrtmp!=0||psdtmp!=0)
	{
		ack.number=0;
	}
	else ack.number=1;
	pack.opt_code=ack.opt_code;
	pack.datas[0]=(char)(ack.number+48);
	pack.datas[1]='\0';
	ret=write(sock,&pack,(strlen(pack.datas)+2));
	//printf("%d\n",ret);
}

int do_data(PK_COMMON *cm_pack,void **linklist)
{
}

int do_ack(PK_COMMON *cm_pack)
{
}

int do_error(PK_COMMON *cm_pack,MSG_ERROR *error,LOG_LIST **linklist)
{
}

int do_iflive(PK_COMMON *cm_pack)
{
}

/*void *thread_send_fun(void *arg)
{
	SOCK_PACK * sended=(SOCK_PACK *)arg;
	int ret=-1;
	do{
		ret=write(sended->sock_word,&sended->package,sizeof(sended->package));
		if(ret<0)
		{
			perror("error send - ");
			return;
		}
	}while(ret>0)
}*/

ssize_t readline(int fd,void *buf,int size)
{
	char *strtmp=NULL;
	ssize_t reval;
	ssize_t realcount=0;
	strtmp=(char *)buf;
	
	while(size>0)
	{
		reval=read(fd,strtmp,1);
		if(reval<0)
		{
			if(errno == EINTR)
				continue;
			else
				return -1;
		}
		else if(reval == 0)
			break;
		else
		{
			realcount++;
			size--;
			if(*strtmp == '\n')
				break;
			strtmp++;
		}
	}
	*strtmp='\0';
	return realcount;
}

ssize_t writeline(int fd,const void *buf,int size)
{
	char *strtmp=NULL;
	ssize_t reval;
	ssize_t realcount=0;
	strtmp=(char *)buf;
	while(size>0)
	{
		reval=write(fd,strtmp,1);
		if(reval<0)
		{
			if(errno == EINTR)
				continue;
			else
				return -1;
		}
		else if(reval==0)
			break;
		else
		{
			realcount++;
			size--;
			strtmp++;
		}
	}
	write(fd,"\n",sizeof("\n"));
	return realcount;
}

/*int upack_package(const PK_COMMON *cm_pack,void *package)
{
	PK_REQUEST *request;
	PK_DATA *data;
	PK_ACK *ack;
	PK_ERROR *error;
	PK_IFLIVE *iflive;
	switch(cm_pack->opt_code)
	{
		case 1:
			request=(PK_REQUEST *)cm_pack;
			package=(void *)request;
			break;
		case 2:
			data=(PK_DATA *)cm_pack;
			package=(void *)data;
			break;
		case 3:
			ack=(PK_ACK *)cm_pack;
			package=(void *)ack;
			break;
		case 4:
			error=(PK_ERROR *)cm_pack;
			package=(void *)error;
			break;
		case 5:
			iflive=(PK_IFLIVE *)cm_pack;
			package=(void *)iflive;
			break;
		default:
			break;
	}
	return cm_pack->opt_code;
}*/

int live_confirm(int socketfd,const PK_IFLIVE *live,int pack_size)
{
	int ret=-1;
	ret=write(socketfd,(struct pk_common*)live,pack_size);
	if(ret<0)
		return -1;
	return 0;
}

int thread_pool_init(TIDPOOL *tids,int maxlen)
{
	int i;
	for(i=0;i<maxlen;i++)
	{
		tids[i].tid=-1;
		tids[i].number=i;
	}
	return 0;
}

int server_init()
{
	int fd;
	char temp[50];
	char buf[50];
	char data[50];
	int flag=0;
	int ret;
	int i;
	memset(&setting,0,sizeof(struct ini_settings));
	setting.ipaddr_port.sin_family = AF_INET;
	fd=open("configure.ini",O_RDONLY);
        
	do{
		ret=-1;
		memset(&buf,0,sizeof(buf));
		ret=readline(fd,buf,sizeof(buf));

		if(ret<0)
		{close(fd);return -1;}
		if(ret==0)
			break;
		flag=0;
		memset(&temp,0,sizeof(temp));
		memset(&data,0,sizeof(data));
		for(i=0;i<50;i++)
		{
			if(buf[i]!='\0')
			{
				if(buf[i]==' ')
					continue;
				if(buf[i]!='='&&flag==0)
				{
					strncat(temp,&buf[i],1);
				}
				if(buf[i]=='='&&flag==0)
				{flag=1;continue;}
				if(buf[i]!='\0'&&flag==1)
				{
					strncat(data,&buf[i],1);
				}
			}
			else break;
		}
		
		if(strcmp(temp,"ipaddress")==0)
		{		
			ret=inet_pton(AF_INET,data,&setting.ipaddr_port.sin_addr.s_addr);
			if(ret<0)
			{close(fd);return -1;}
		}
				
		if(strcmp(temp,"port")==0)
			setting.ipaddr_port.sin_port = htons(*((int*)data));

		if(strcmp(temp,"MAX_CONNECTING")==0)
			setting.connect_max=*((int *)data);

		if(strcmp(temp,"username")==0)
			strcpy(setting.usrname,data);

		if(strcmp(temp,"passwd")==0)
			strcpy(setting.psd,data);

		if(strcmp(temp,"file_path")==0)
			strcpy(setting.file_path,data);

		if(strcmp(temp,"level")==0) setting.log_level=*((int *)data);
	}while(ret>0);
	close(fd);
	return 0;
}
